<?php
include 'user_detail_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/select2/select2_metro.css' ),
    array ( 'path' => 'plugins/data-tables/DT_bootstrap.css' ),
    array ( 'path' => 'css/pages/nh-user-detail.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/select2/select2.min.js' ),
    array ( 'path' => 'plugins/data-tables/jquery.dataTables.min.js' ),
    array ( 'path' => 'plugins/data-tables/DT_bootstrap.js' ),
    array ( 'path' => 'scripts/nh-user-detail-diary.js' ),
);

$a_js_class[] = 'User_Diary_Loader.init();';

$a_GLV_User = $a_Tmp_Dot_KiemTra = $a_Tmp_SoLan_KiemTra = $a_Sundays = $a_ChuyenCan = array ();

// <editor-fold defaultstate="collapsed" desc="Get Data">
if ( !$f_course || !$f_userid ) { raise_error('Chưa được phân lớp.'); }

$d_user = get_user_class(array($f_userid), array($f_course), $db_conn);
$d_user = $d_user[$f_userid][$f_course];

// <editor-fold defaultstate="collapsed" desc="Get Score">
$v_tmp_startdate = date( $d_user->course + 0 . '-m-d', strtotime($a_KhoaHoc_Info['start']));
$v_tmp_enddate   = date( $d_user->course + 1 . '-m-d', strtotime($a_KhoaHoc_Info['end'  ]));

$db_query = "
    select  userid, scoretype, data, score, date
    from    user_score
    where   userid = '$f_userid' and date >= '$v_tmp_startdate' and date <= '$v_tmp_enddate'
    order by date";

$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {

    if (in_array($d_row->scoretype, array_keys($a_Loai_ChuyenCan))) {
        $a_ChuyenCan[$d_row->date][$d_row->scoretype] = $d_row->score;
        $a_Sundays[$d_row->date]                        = $d_row->date;
    }
    else {
        $a_scores[$d_row->data][$d_row->scoretype] = $d_row->score;
        $a_Tmp_SoLan_KiemTra[$d_row->data]         = $d_row->data;
        $a_Tmp_Dot_KiemTra[$d_row->scoretype]      = $d_row->scoretype;
    }
}
// </editor-fold>

ksort($a_Tmp_SoLan_KiemTra);
ksort($a_Tmp_Dot_KiemTra);

// <editor-fold defaultstate="collapsed" desc="Get User Info">
$a_tmp_option = array(
    'class_condition'=> "course = '" . $d_user->course . "' and team = '$d_user->team' and level = '$d_user->level' and crew = '$d_user->crew'",
    'user_condition' => "userid = '$f_userid' or usertype = 'HUYNH_TRUONG'",
);

$a_tmp_user = get_class_to_user($a_tmp_option, $db_conn);

foreach ($a_tmp_user as $key => $d_tmp_user) {
    if ($d_tmp_user->usertype == 'HUYNH_TRUONG') { $a_GLV_User[$key] = $d_tmp_user->saint . ' ' . $d_tmp_user->username; }
    
    if ($key == $f_userid) { $d_user = $d_tmp_user; }
}

unset($a_tmp_user, $a_tmp_option);
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Not have Data yet">
if (!$d_user->rank) {
    $d_user->hocluc   = 10;
    $d_user->chuyencan = 10;
}
// </editor-fold>

$o_rank_chuyencan = get_rank_type($d_user->chuyencan, 'CHUYEN_CAN');
$o_rank_hocluc   = get_rank_type($d_user->hocluc, 'HOC_LUC');
// </editor-fold>

include 'lib_head.php';
?>

<div class="row-fluid profile">
    <ul class="unstyled profile-nav span4">
        <li><img src="user_image.php?action=download-image&amp;userid=<?=$f_userid?>"/></li>
        <li>
            <a>
                <i class="icon-bookmark"></i> 
                Khóa <?=$d_user->course?> - 
                <?= isset($a_Nganh  [$d_user->team      ]) ? '   ' . $a_Nganh   [$d_user->team ] : '' ?>
                <?= isset($a_Cap    [$d_user->level     ]) ? ' - ' . $a_Cap     [$d_user->level] : '' ?>
                <?= isset($a_Doi    [$d_user->crew      ]) ? ' - ' . $a_Doi     [$d_user->crew ] : '' ?>
            </a>
        </li>
        <?php foreach ($a_GLV_User as $v_name) {?>
        <li><a><i class="icon-user"></i> <?=$v_name?> <span>HT</span></a></li>
        <?php } ?>
        <li><a><i class="icon-calendar"></i> Chuyên Cần <span><?=$d_user->chuyencan?></span></a></li>
        <li><a><i class="icon-star"></i> Học Lực <span><?=$d_user->hocluc?></span></a></li>
        <?php if ($d_user->rank) :?>
        <li><a><i class="icon-trophy"></i> Tổng Kết<span><?=@$a_XepHang[$d_user->rank]?></span></a></li>
        <?php endif;?>
    </ul>
    <div class="span8">
        <div class="row-fluid">
            <div class="profile-info form-section">
                <h1><?=$d_user->saint?> <?=$d_user->username?></h1>
                <p><?=$d_user->note ? 'Huynh Trưởng nhận xét: ' . $d_user->note : ''?></p>
                <ul class="unstyled">
                </ul>
            </div>
        </div>
        <div class="row-fluid">
            <div class="portlet box purple">
                <div class="portlet-title">
                    <div class="caption"><i class="icon-globe"></i> Chuyên Cần <?=$d_user->chuyencan?> - <?=$o_rank_chuyencan->value?></div>
                </div>
                <div class="portlet-body">
                    <table class="table table-striped table-bordered table-hover table-full-width" id="chuyencan_table">
                        <thead>
                            <tr>
                                <th>Ngày</th>
                                <th>Đi Lễ</th>
                                <th>Học GL</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $o_rank_chuyencan = get_rank_type($d_user->chuyencan, 'CHUYEN_CAN');

                            foreach ($a_Sundays as $v_sundate) {
                                $v_tmp_le  = isset($a_ChuyenCan[$v_sundate]['LE' ]) ? $a_ChuyenCan[$v_sundate]['LE' ] : '';
                                $v_tmp_hoc = isset($a_ChuyenCan[$v_sundate]['HOC']) ? $a_ChuyenCan[$v_sundate]['HOC'] : '';
                                ?>
                                <tr>
                                    <td class="center"><?=$v_sundate            ?></td>
                                    <td class="center"><?=@$a_DiLe [$v_tmp_le ] ?></td>
                                    <td class="center"><?=@$a_DiHoc[$v_tmp_hoc] ?></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="row-fluid">
            <div class="portlet box green">
                <div class="portlet-title">
                    <div class="caption"><i class="icon-globe"></i>Học Lực <?=$d_user->hocluc?> - <?=$o_rank_hocluc->value?></div>
                </div>
                <div class="portlet-body">
                    <table class="table table-striped table-bordered table-hover table-full-width" id="hocluc_table">
                        <thead>
                            <tr>
                                <th>Kiểm Tra</th>
                                <?php foreach ($a_Tmp_SoLan_KiemTra as $v_value) { ?>
                                <th><?=@$a_SoLan_KiemTra[$v_value]?></th>
                                <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $o_rank_hocluc = get_rank_type($d_user->hocluc, 'HOC_LUC');
                            foreach ($a_Tmp_Dot_KiemTra as $v_scoretype_key) { ?>
                            <tr>
                                <td class="center"><?= @$a_Dot_KiemTra[$v_scoretype_key] ?></td>
                                <?php foreach ($a_Tmp_SoLan_KiemTra as $v_value) { ?>
                                <td class="center"><?= @$a_scores[$v_value][$v_scoretype_key] ?></td>
                                <?php } ?>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include 'lib_tail.php'; ?>